package com.situ.car.dao.Impl;

import com.situ.car.dao.IUserDao;
import com.situ.car.pojo.entity.Admin;
import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.entity.User;
import com.situ.car.pojo.entity.UserData;
import com.situ.car.pojo.query.UserQuery;
import com.situ.car.pojo.vo.AdminVo;
import com.situ.car.util.JDBCUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<User> selectByPage(UserQuery userQuery) {
        String sql = "select * from user where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryUsername = userQuery.getUsername();
        if (!ObjectUtils.isEmpty(queryUsername)) {
            sql += " and username like ?";
            queryList.add("%" + queryUsername + "%");
        }
        String queryAccount = userQuery.getAccount();
        if (!ObjectUtils.isEmpty(queryAccount)) {
            sql += " and account like ?";
            queryList.add("%" + queryAccount + "%");
        }
        sql += " limit ?,?";
        int offset = (userQuery.getPage() - 1) * userQuery.getLimit();
        queryList.add(offset);
        queryList.add(userQuery.getLimit());
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(UserQuery userQuery) {
        String sql = "select count(*) from user where 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryUsername = userQuery.getUsername();
        if (!ObjectUtils.isEmpty(queryUsername)) {
            sql += " and username like ?";
            queryList.add("%" + queryUsername + "%");
        }
        String queryAccount = userQuery.getAccount();
        if (!ObjectUtils.isEmpty(queryAccount)) {
            sql += " and account like ?";
            queryList.add("%" + queryAccount + "%");
        }
        Integer count = template.queryForObject(sql, Integer.class, queryList.toArray());
        return count;
    }

    @Override
    public void deleteById(Integer id) {
        template.update("delete from user where id=?", id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        template.update("update user set status=? where id=?", status, id);
    }

    @Override
    public Integer selectByAccount(User user) {
        return template.queryForObject("select count(*) from user where account=?", Integer.class,user.getAccount());
    }

    @Override
    public void add(User user) {
        template.update("insert into user (username, account, password, avatar, gender, age, email, phone) VALUES (?,?,?,?,?,?,?,?)",
                user.getUsername(), user.getAccount(), user.getPassword(), user.getAvatar(), user.getGender(), user.getAge(), user.getEmail(), user.getPhone());
    }

    @Override
    public User selectById(Integer id) {
        return template.queryForObject("SELECT id, username, account, password, avatar, gender, age, email, status, phone FROM user where id=?", new BeanPropertyRowMapper<>(User.class), id);
    }

    @Override
    public int update(User user) {
        try {
            return template.update("update user set username=?, account=?, password=?, avatar=?, gender=?, age=?, email=?, phone=? where id=?",user.getUsername(), user.getAccount(), user.getPassword(), user.getAvatar(), user.getGender(), user.getAge(), user.getEmail(), user.getPhone(), user.getId());
        } catch (DuplicateKeyException e) {
            return 0;
        }
    }

    @Override
    public List<User> selectOrder() {
        return template.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public Integer totalCost() {
        Integer count = template.queryForObject("select count(*) from user", Integer.class);
        return count;
    }

    @Override
    public List<UserData> selectAll() {
        List<UserData> list = template.query("select * from user", new BeanPropertyRowMapper<>(UserData.class));
        return list;
    }

    @Override
    public void addExcel(UserData userData) {
        template.update("insert into user (username, account, password, avatar, gender, age, email, phone) VALUES (?,?,?,?,?,?,?,?)",
                userData.getUsername(), userData.getAccount(), userData.getPassword(), userData.getAvatar(), userData.getGender(), userData.getAge(), userData.getEmail(), userData.getPhone());
    }


}
